package com.navy.client.login;

import java.io.Serializable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ProgressBar;

import com.navy.common.Icon;

/**
 * @ClassName: 用户登录界面
 * @Description:
 * @author chenhaijun
 * @date 2013-6-1 上午11:25:35
 */
public class ClinetLogin implements Serializable
{

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 1L;

    private static Text username;
    private static Text password;

    /**
     * @Title: main
     * @Description:
     * @param @param args 设定文件
     * @return void 返回类型
     * @throws
     */
    public static void main(String[] args)
    {

        Display display = Display.getDefault();
        final Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.RESIZE);
        final Canvas canvas = new Canvas(shell, SWT.ALL);
        canvas.setVisible(false);

        shell.setBackground(SWTResourceManager.getColor(240, 240, 255));
        shell.setSize(313, 214);
        shell.setText("用户登录");
        shell.setLocation(display.getClientArea().width / 2 - shell.getSize().x / 2, display.getClientArea().height / 2
                - shell.getSize().y / 2);

        Image image = Icon.getImage("/img/icon/loginicon.png");
        shell.setImage(image);

        username = new Text(shell, SWT.BORDER);
        username.setBounds(116, 32, 150, 19);

        password = new Text(shell, SWT.BORDER);
        password.setBounds(116, 68, 150, 19);

        Label userLabel = new Label(shell, SWT.NONE);
        userLabel.setBounds(43, 35, 49, 14);
        userLabel.setText("用户名：");

        Label pwdLabel = new Label(shell, SWT.NONE);
        pwdLabel.setBounds(43, 71, 49, 14);
        pwdLabel.setText("密    码：");

        final Button login_button = new Button(shell, SWT.NONE);
        login_button.setBounds(43, 114, 68, 23);
        login_button.setText("登 录 ");
        login_button.setImage(Icon.getImage("/img/icon/user.gif"));

        final Button canel_button = new Button(shell, SWT.NONE);
        canel_button.setBounds(198, 114, 68, 23);
        canel_button.setText("关 闭 ");
        canel_button.setImage(Icon.getImage("/img/icon/closeone.png"));

        // 点击登录
        login_button.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent selectionEvent)
            {
                // 判断用户名密码不为空
                if ("".equals(username.getText()) || "".equals(password.getText()))
                {
                    MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                    dialog.setText("提示");
                    dialog.setMessage("请输入用户名和密码！");
                    dialog.open();
                    if ("".equals(username.getText()))
                    {
                        username.setFocus();
                    }
                    else
                    {
                        password.setFocus();
                    }
                    return;
                }
                Label wait = new Label(canvas, SWT.NONE);
                wait.setBounds(95, 60, 200, 14);
                wait.setText("正在登录，请稍候...");
                canvas.setBounds(0, 0, shell.getSize().x, shell.getSize().y);

                final ProgressBar progressBar = new ProgressBar(canvas, SWT.Arm);
                progressBar.setBounds(68, 100, 170, 17);
                canvas.setVisible(true);
            }
        });

        // 取消 关闭
        canel_button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                shell.dispose();
            }
        });

        // 回车键 提交
        KeyListener k = new KeyAdapter()
        {
            public void keyPressed(final KeyEvent e)
            {
                if (13 == e.keyCode || 16777296 == e.keyCode)
                {
                    login_button.notifyListeners(SWT.Selection, new Event());
                }
            }
        };
        shell.addKeyListener(k);
        username.addKeyListener(k);
        password.addKeyListener(k);
        shell.open();
        shell.layout();

        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
        shell.dispose();

    }
}